//
//  MeVC.m
//  EducationLive
//
//  Created by Apple on 2017/9/26.
//  Copyright © 2017年 ZhouHao. All rights reserved.
//

#import "MeVC.h"
#import "MeHeaderView.h"
#import "MXParallaxHeader.h"
#import "MeModel.h"
#import "MeCellNode.h"
#import "LoginVC.h"
#import "PersonalVC.h"
#import "SubscribeVC.h"

@interface MeVC ()<ASTableDelegate, ASTableDataSource>
// UI
@property (nonatomic, strong) ASTableNode *tableNode;
@property (nonatomic, weak) MeHeaderView *headerView;
//Data
@property (nonatomic, strong) NSArray *titleDataArray;
@property (nonatomic, strong) NSArray *imageDataArray;

@end

#define kHeaderViewHeight 175

@implementation MeVC

#pragma mark - life cycle

- (instancetype)init
{
    if (self = [super initWithNode:[ASDisplayNode new]]) {
        [self addTableNode];
    }
    return self;
}

- (void)addTableNode
{
    _tableNode = [[ASTableNode alloc] init];
    _tableNode.backgroundColor = RGB_255(245,245,245);
    _tableNode.delegate = self;
    _tableNode.dataSource = self;
    [self.node addSubnode:_tableNode];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self tableListData];
    // 头视图Frame更新
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    [self.view layoutIfNeeded];
    // 加载头视图
    [self addTableHeaderView];
    // 加载表Footer
    [self configureTableView];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];

     [self.navigationController setNavigationBarHidden:YES animated:animated];
}

- (void)viewWillLayoutSubviews
{
    [super viewWillLayoutSubviews];
    _tableNode.frame = self.node.bounds;
}
// 加载表Footer
- (void)configureTableView
{
    _tableNode.view.tableFooterView = [[UIView alloc]init];
    _tableNode.view.separatorStyle = UITableViewCellSeparatorStyleNone;
}
#pragma mark - TableHeaderView TableFooterView
// 加载头视图
- (void)addTableHeaderView
{
    MeHeaderView *headerView = [MeHeaderView loadInstanceFromNib];
    [headerView.headImageBtn addTarget:self action:@selector(headImageAction:) forControlEvents:UIControlEventTouchUpInside];
    // 圆角
//    headerView.headImageView.layer.cornerRadius = 30;
//    headerView.headImageView.clipsToBounds = YES;
    _tableNode.view.parallaxHeader.view = headerView;
    _tableNode.view.parallaxHeader.height = kHeaderViewHeight;
    _tableNode.view.parallaxHeader.mode = MXParallaxHeaderModeFill;
    _tableNode.view.parallaxHeader.contentView.layer.zPosition = 1;
    _headerView = headerView;
}
// 头像点击
- (void)headImageAction:(UIButton *)button
{
    PersonalVC *personalVC = [[PersonalVC alloc]init];
    [self.navigationController pushViewController:personalVC animated:YES];
}
#pragma mark - load Data
- (void)loadData
{
    
}
// 表数据
- (void)tableListData
{
    _titleDataArray = @[@[@"我的账户",@"我的订阅",@"订单管理",@"资质证明",@"意见反馈"],@[@"积分商城",@"设置"]];
    _imageDataArray = @[@[@"me_zhanghu",@"me_dingyue",@"me_dingdan",@"me_zizhi",@"me_yijian"],@[@"me_jifen",@"me_sheji"]];
}

#pragma mark - ASTableDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return _titleDataArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSArray *titleDataArray = _titleDataArray[section];
    return titleDataArray.count;
}

- (ASCellNodeBlock)tableView:(ASTableView *)tableView nodeBlockForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 数据
    MeModel *meModel = [[MeModel alloc]init];
    meModel.iconName =  _titleDataArray[indexPath.section][indexPath.row];
    meModel.iconImage =  _imageDataArray[indexPath.section][indexPath.row];
    
    ASCellNode *(^cellNodeBlock)() = ^ASCellNode *() {
        MeCellNode *cellNode = [[MeCellNode alloc] initWithItem:meModel];
        return cellNode;
    };
    return cellNodeBlock;
}
#pragma mark - ASTableDelegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        LoginVC *loginVC = [[LoginVC alloc]init];
        [self.navigationController pushViewController:loginVC animated:YES];
    }else if (indexPath.row == 1) {
        SubscribeVC *subscribeVC = [[SubscribeVC alloc]init];
        [self.navigationController pushViewController:subscribeVC animated:YES];
    }else{
        
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return 0;
    }
    return 10;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *headView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 10)];
    headView.backgroundColor = RGB_255(245,245,245);
    return headView;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
